<?php
/**
 * Handles and manages an company and its entity in database
 * - based on CRUD
 *
 */
class KommunModel extends Model {

    public function createKommun(){
    }

    public function readKommun(){
    }

    public function updateKommun(){
    }

    public function deleteKommun(){
    }


    /*
     *  Relations
     */

    /* Kommun_page (Municipality)*/
    public function createMuniPage(){
        
    }

    public function readMuniPage_numeric($mID){
        $result = $this->db->query('
            SELECT c.id as county_id, c.name as county_name , m.name as muni_name,m.id, mp.*
            FROM counties c,municipalities m LEFT JOIN municipalitie_page mp
            ON m.id = mp.municipalities_id
            WHERE m.id = ?
            AND c.id = m.county_id
           ','i',$mID);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }
    public function readMuniPage_string($mID){
        $result = $this->db->query('
            SELECT c.id as county_id, c.name as county_name , m.name as muni_name,m.id, mp.*
            FROM counties c,municipalities m LEFT JOIN municipalitie_page mp
            ON m.id = mp.municipalities_id
            WHERE m.name = ?
            AND c.id = m.county_id
           ','s',$mID);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }
    
    public function updateMuniPage($id){
    }

    public function deleteMuniPage($id){
    }


     /* listCountyPage Lists all counties*/
    public function listCountyPage(){
        $result = $this->db->query('
            SELECT c.id, c.name
            FROM counties c
            ORDER BY c.name');

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

    /* listMuniPage Lists all muni in a county*/
    public function listMuniPage_numeric($cID){
        $result = $this->db->query('
            SELECT c.name as county_name, m.id, m.name
            FROM counties c, municipalities m
            WHERE c.id = m.county_id
            AND c.id = ?
            ORDER BY m.name','i',$cID);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

    /* listMuniPage Lists all muni in a county*/
    public function listMuniPage_string($cID){
        $result = $this->db->query('
            SELECT c.name as county_name, m.id, m.name
            FROM counties c, municipalities m
            WHERE c.id = m.county_id
            AND c.name = ?
            ORDER BY m.name','s',$cID);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

       public function searchMuniNews($searchStr){
        $result = $this->db->query("
            SELECT *
            FROM news
            WHERE title LIKE CONCAT('%', ?, '%')
            OR text LIKE CONCAT('%', ?, '%')
            OR text2 LIKE CONCAT('%', ?, '%')
            ", 'sss', $searchStr, $searchStr, $searchStr);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

    public function searchMuniVideos($searchStr){
        $result = $this->db->query("
            SELECT *
            FROM videos
            WHERE title LIKE CONCAT('%', ?, '%')
            OR description LIKE CONCAT('%', ?, '%')
            OR description2 LIKE CONCAT('%', ?, '%')
            ", 'sss', $searchStr, $searchStr, $searchStr);

        if(count($result) > 0)
            return $result;
        else
            return false;
    }

}
?>
